package club.gskj.gulimall.product.service.impl;

import club.gskj.gulimall.product.entity.AttrEntity;
import club.gskj.gulimall.product.service.AttrService;
import club.gskj.gulimall.product.vo.AttrGroupWithAttrsVo;
import club.gskj.gulimall.product.vo.SkuItemVo;
import club.gskj.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.Query;

import club.gskj.gulimall.product.dao.AttrGroupDao;
import club.gskj.gulimall.product.entity.AttrGroupEntity;
import club.gskj.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        // 判断是否带有 检索字段 key
        Object key = params.get("key");

        // 查询条件
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (catelogId != null && catelogId > 0) {
            wrapper.eq("catelog_id", catelogId);
        }

        if (!StringUtils.isEmpty(key)) {
            wrapper.and((wra) -> {
                wra.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> listAttrByAttrGroupByCatId(Long catId) {

        // 查询分组
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));

        // 查询分组下的所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntityList.stream().map(group -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vo);

            List<AttrEntity> attrEntities = attrService.ListRelationAttr(group.getAttrGroupId());

            vo.setAttrs(attrEntities);

            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> listAttrGroupWithAttrsBySpuId(Long spuId, Long catelogId) {
        // 1. 查询当前spu对应的所有属性的分组以及该分组下的所有属性对应的值
        List<SpuItemAttrGroupVo> vos = this.getBaseMapper().listAttrGroupWithAttrsBySpuId(spuId, catelogId);
        return vos;
    }

}
